<?php
/**
* Add some WP Admin customization
*/

if (!class_exists('bibliotype')) {
    class bibliotype {
        //This is where the class variables go, don't forget to use @var to tell what they're for
        /**
        * @var string The options string name for this plugin
        */
        var $optionsName = 'bibliotype_options';
        
        /**
        * @var string $localizationDomain Domain used for localization
        */
        var $localizationDomain = "bibliotype";
        
        /**
        * @var string $pluginurl The path to this plugin
        */ 
        var $thispluginurl = '';
        /**
        * @var string $pluginurlpath The path to this plugin
        */
        var $thispluginpath = '';
            
        /**
        * @var array $options Stores the options for this plugin
        */
        var $options = array();
        
        //Class Functions
        /**
        * PHP 4 Compatible Constructor
        */
        function bibliotype(){$this->__construct();}
        
        /**
        * PHP 5 Constructor
        */        
        function __construct(){
            //Initialize the options
            $this->getOptions();
            
            //Actions        
            add_action("admin_menu", array(&$this,"admin_menu_link"));
        }
        
        /**
        * Retrieves the plugin options from the database.
        * @return array
        */
        function getOptions() {
            //Don't forget to set up the default options
            if (!$theOptions = get_option($this->optionsName)) {
                $theOptions = array('default'=>'options');
                update_option($this->optionsName, $theOptions);
            }
            $this->options = $theOptions;
        }
        /**
        * Saves the admin options to the database.
        */
        function saveAdminOptions(){
            return update_option($this->optionsName, $this->options);
        }
        
        /**
        * @desc Adds the options subpanel
        */
        function admin_menu_link() {
			add_submenu_page( 'themes.php', 'Bibliotype Settings', 'Bibliotype', 10, basename(__FILE__), array(&$this,'admin_options_page'));
        }

        /**
        * Adds settings/options page
        */
        function admin_options_page() { 
            if($_POST['bibliotype_save']){
                if (! wp_verify_nonce($_POST['_wpnonce'], 'bibliotype-update-options') ) die('Whoops! There was a problem with the data you posted. Please go back and try again.'); 
                $this->options['bibliotype_title'] = $_POST['bibliotype_title'];                   
                $this->options['bibliotype_author'] = $_POST['bibliotype_author'];
                $this->options['bibliotype_alert'] = $_POST['bibliotype_alert'];
                $this->options['bibliotype_debug_enabled'] = ($_POST['bibliotype_debug_enabled']=='on')?true:false;
  
                $this->saveAdminOptions();
                
                echo '<div class="updated"><p>Success! Your changes were sucessfully saved!</p></div>';
            }
?>                                   
                <div class="wrap">
				<div class="icon32" id="icon-tools"> <br /> </div>
                <h2>Bibliotype Options</h2>
                <form method="post" id="bibliotype_options">
                <?php wp_nonce_field('bibliotype-update-options'); ?>
                    <table width="100%" cellspacing="2" cellpadding="5" class="form-table"> 
                        <tr valign="top"> 
                            <th width="33%" scope="row"><?php _e('Title:', $this->localizationDomain); ?></th> 
                            <td><input name="bibliotype_title" type="text" id="bibliotype_title" size="45" value="<?php echo $this->options['bibliotype_title'] ;?>"/>
                        </td> 
                        </tr>
                        <tr valign="top"> 
                            <th width="33%" scope="row"><?php _e('Author:', $this->localizationDomain); ?></th> 
                            <td><input name="bibliotype_author" type="text" id="bibliotype_author" value="<?php echo $this->options['bibliotype_author'] ;?>"/>
                            </td> 
                        </tr>
                        <tr valign="top"> 
                            <th width="33%" scope="row"><?php _e('Non-tablet Alert Message:', $this->localizationDomain); ?></th> 
                            <td><input name="bibliotype_alert" type="text" id="bibliotype_alert" size="75" value="<?php echo $this->options['bibliotype_alert'] ;?>"/>
                            </td> 
                        </tr>

				      <tr valign="top"> 
				          <th><label for="bibliotype_debug_enabled"><?php _e('Enable Debug Mode:', $this->localizationDomain); ?></label></th><td><input type="checkbox" id="bibliotype_debug_enabled" name="bibliotype_debug_enabled" <?=($this->options['bibliotype_debug_enabled']==true)?'checked="checked"':''?>> <br />
				          	With debug mode enabled swipe right to reload the page, swipe left to toggle the grid.</td>
				      </tr>
                        <tr>
                            <th colspan=2><input type="submit" class="button-primary" name="bibliotype_save" value="Save" /></th>
                        </tr>
                    </table>
                </form>
                <?php
        }
        
  } //End Class
} //End if class exists statement

//instantiate the class
if (class_exists('bibliotype')) {
    $bibliotype_var = new bibliotype();
}

?>